<template>
	<view class="msgcenter">
		<u-swipe-action :show="item.show" :index="index" 
			v-for="(item, index) in ffList" :key="index" 
			@click="msgclick" @open="msgopen"
			:options="msgoptions"
		>
			<view class="item" @click="goTalk(index)">
				<view class="head flex-col flex-c">
					<u-avatar :src="item.sumb_headimg | formatImage"></u-avatar>
					<u-badge type="error" :offset="[0,0]" :count="item.count"></u-badge>
				</view>
				<!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
				<view class="content-wrap">
					<view class="top flex-c flex-bt">
						<view class="nickname">{{item.name}}</view>
						<view class="time">{{item.addtime}}</view>
					</view>
					<view class="content">{{item.content | clearHtml}}</view>
				</view>
			</view>
		</u-swipe-action>
		<!-- <u-tabbar :border-top="false" bg-color="#1B1A1F" active-color="#A87B38" inactive-color="#CCC" v-model="current" :list="list" :mid-button="false"></u-tabbar> -->
	</view>
</template>

<script>
	import {WS} from '@/common/WS.js'
	import store from '@/store/index.js';
	export default {
		data() {
			return {
				ffList: [],
				ffList2: [],
				msgoptions: [
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
				ws: null,
				kefu: 0
			}
		},
		onLoad() {
			console.log("聊天系统")
			this.openService()
			
			setTimeout(()=>{
				this.getNowMsgList()
			},0)
		},
		onShow() {
			if(!this.$store.state.hasLogin){
				uni.redirectTo({
					url:'/pages/appview/index'
				})
			}
			this.getmsg()
			setTimeout(()=>{
				this.getNowMsgList()
			},0)
		},
		methods: {
			getmsg(){
				var _this = this
				this.ws = new WS(2)
				console.log(this.ws)
				this.ws.open(()=>{
					_this.ws.send(JSON.stringify({
						userid: _this.$store.state.userid
					}))
				})
				this.ws.get(function (rets) {
					if (rets.indexOf('action') != -1) {
						rets = JSON.parse(rets);
						if (rets.code == 200) {
							if (rets.action == 'Home/Privatechat/getOtherList') {
								var result = rets.data.result.result;
								_this.ffList = result;
								uni.pageScrollTo({
									scrollTop:0
								})
							}
							if (rets.action == 'Home/User/syslist') {
								_this.ffList2 = rets.data;
							}
							if (rets.action == 'Home/Privatechat/getreadcount') {
								var result = rets.data;
								var msgNum = {
									cnum: result.chat_count,
									mnum: result.message_count
								}
								_this.$store.commit('msgNum', msgNum);
								return;
							}
						}
					}
				})
			},
			msgdetail(){
				uni.navigateTo({
					url:'/pages/message/index'
				})
			},
			async openService() {
				const {data:res} = await this.$Request({
					url: 'api/Member/kefu'
				})
				if (res && res.data && res.data.id) {
					this.kefu = res.data.id
				}
			},
            goTalk: function (index) {
                var _this = this;
                var data = _this.ffList[index];
                var uid = data.userid;
                var name = data.name;
				if(_this.ffList[index].type==-2){
					return uni.navigateTo({
						url:'/pages/collect/index?types=1&visitor=1'
					})
				}

                if (this.kefu == this.$store.state.userid) {
                    // 如果自己是客服
					uni.navigateTo({
						url:'/pages/message/index?toid=' + uid + '&name=' + name
					})
                } else {
                    // 如果对方是客服
                    if (this.kefu == uid) {
						uni.navigateTo({
							url:'/pages/message/index?toid=' + uid + '&name=客服'
						})
                    } else {
                        // 男性会员 免费回复
                        if (this.$store.state.sex == '男' && this.$store.state.isVip == 1) {
							uni.navigateTo({
								url:'/pages/message/index?toid=' + uid + '&name=' + name
							})
                            return;
                        }
                        // 认证女性 免费回复
                        if (this.$store.state.sex == '女' && this.$store.state.rz == 2) {
							uni.navigateTo({
								url:'/pages/message/index?toid=' + uid + '&name=' + name
							})
                            return;
                        }
						this.$judgeQx(uid, function () {
							uni.navigateTo({
								url:'/pages/message/index?toid=' + uid + '&name=' + name
							})
                        }, 1)
                    }
                }
            },
			getNowMsgList(cb){
				if (!this.$store.state.userid) {
					return;
				}
				var obj = {
					userid: this.$store.state.userid
				};
				this.$Request({
					url:'apis/Home/User/syslist',
					data:{
						user_id: this.$store.state.userid
					}
				})
				this.$Request({
					url:'apis/Home/Privatechat/getOtherList',
					data:{
						data: JSON.stringify(obj)
					}
				})
				typeof cb == 'function' && cb()
			},
			async msg(id, name){
                var _this = this;
				const {data:res} = await this.$Request({
					url: 'apis/Home/User/isread',
					data: {
						user_id: this.$store.state.userid,
						id: id
					}
				})
				this.$pushMsg(this.$store.state.userid);
				uni.navigateTo({
					url:'/pages/sysmsg/index?id=' + id + '&title=' + name
				})
			},
			msgclick(index, index1) {
				this.ffList.splice(index, 1);
				this.$u.toast(`删除了第${index}个cell`);
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			msgopen(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.ffList[index].show = true;
				this.ffList.map((val, idx) => {
					if(index != idx) this.ffList[idx].show = false;
				})
			}
		},
		computed:{
			list(){
				return this.$store.state.tabbarlist
			},
			current(){
				return this.$store.state.tabbarcurrent
			},
			myinfo(){
				return this.$store.state.userinfo
			}
		},
		watch:{
			'myinfo.id': {
				handler(){
					this.ws.close()
					this.ws = new WS(2)
				}
			}
		},
		onPullDownRefresh() {
			this.getNowMsgList(()=>{
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss" scoped>
	.msgcenter {
		background-color: #2A2931;
		
		min-height: 100vh;
		
		min-height: calc(100vh - 44px);
		
		.toptop{
			width: 100vw;
			padding: 40rpx 0;
			.topitem{
				position: relative;
			}
			text{
				color: #999;
				margin-top: 10rpx;
			}
		}
		.item {
			background-color: #2A2931;
			display: flex;
			padding: 20rpx;
			.head{
				position: relative;
			}
			.content-wrap {
				margin-left: 20rpx;
				width: calc(100vw - 150rpx);
				.top{
					margin:10rpx 0;
					width: 100%;
					.nickname{
						color: #fff;
						flex: 1;
						font-weight: bold;
					}
					.time{
						color: #999;
						font-size: 24rpx;
					}
				}
				.content{
					margin-top:10rpx;
					color: #999;
				}
			}
		}
	}
	
</style>
